package com.weirong.smartvault.backservice.util.table;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/29
 * Time: 11:03
 */
public class Table<T> {
    List<TableRow<T>> table = new ArrayList<>();



    public Table(Integer columnCount){
        for (int i = 0; i < columnCount; i++) {
            table.add(new TableRow<>());
        }
    }
    public TableRow<T> get(Integer i){
        return table.get(i);
    }

    public List<Integer> range(){
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < table.get(0).getSize(); i++) {
            res.add(i);
        }
        return res;
    }
    public void setOne(Integer x,String content,Integer height){
        setOne(x,content,height,null);
    }

    public void setOne(Integer x,String content){ // 根据后一级的元素自动计算之前格子的高度
        Integer height;
        if (x>=table.size()-1){ //没有下一级别
            height = 1;
        }else{ //检查后一级别的checked标志
            height = table.get(x+1).getUncheckCountAndSet();
            height = height<1?1:height;
        }
        setOne(x,content,height);
    }
    public void setOne(Integer x,String content,Integer height,T obj,String additionInf){
        height = height==0?1:height;
        Integer increment = table.get(x).Increment(height);
        for (int i = 0; i < increment; i++) {
            addNewLine();
        }
        table.get(x).setCell(content,height,obj,additionInf);
    }
    public void setOne(Integer x,String content,Integer height,T obj){
        setOne(x,content,height,obj,"");
    }

    public void freeze(){ //冻结合并所有非有效单元格
        for (TableRow tableRow:table){
            tableRow.freeze();
        }
    }

    private void addNewLine(){
        for (TableRow aTable : table)
            aTable.addNewCell("", 1);
    }
}
